package pers.vankid.sessionkeeper.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.authc.AuthcUser;
import pers.vankid.common.model.authc.AuthcUserResponse;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.sessionkeeper.service.SessionKeeperService;

@RestController
@RequestMapping("session")
public class SessionKeeperController {
    @Autowired
    private SessionKeeperService sessionKeeperService;

    /**
     * 登陆
     *
     * @param authcUser
     * @return
     */
    @PostMapping("login")
    public CommonResponse login(@RequestBody AuthcUser authcUser) {
        try {
            return CommonResponse.buildAddSuccessResponse(sessionKeeperService.login(authcUser));
        } catch (Exception e) {
            return CommonResponse.buildBadRequestResponse(e.getMessage());
        }
    }

    /**
     * 注销
     *
     * @param sign
     * @return
     */
    @PostMapping("layout/{sign}")
    public CommonResponse layout(@PathVariable String sign) {
        try {
            sessionKeeperService.layout(sign);
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildBadRequestResponse(e.getMessage());
        }
    }

    /**
     * 检查
     *
     * @param sign
     * @return
     */
    @PostMapping("check/{sign}")
    public CommonResponse check(@PathVariable String sign) {
        try {
            return CommonResponse.buildDefaultSuccessResponse(sessionKeeperService.checkLogin(sign));
        } catch (Exception e) {
            return CommonResponse.buildBadRequestResponse(e.getMessage());
        }
    }

    /**
     * 获取登陆用户
     *
     * @param sign
     * @return
     */
    @GetMapping("{sign}")
    public CommonResponse getAuthcData(@PathVariable String sign) {
        try {
            return CommonResponse.buildGetSuccessResponse(sessionKeeperService.getAuthcData(sign));
        } catch (Exception e) {
            return CommonResponse.buildBadRequestResponse(e.getMessage());
        }
    }

    /**
     * 修改登陆用户数据
     *
     * @param sign
     * @return
     */
    @PostMapping("user/{sign}")
    public CommonResponse updateAuthcData(@PathVariable String sign, @RequestBody AuthcUserResponse authcUserResponse) {
        try {
            sessionKeeperService.updateAuthcData(sign, authcUserResponse.getJson());
            return CommonResponse.buildUpdateSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildBadRequestResponse(e.getMessage());
        }
    }
}
